using Grpc.Core;
using Identity.Application.Contracts.Services;
using PermissionCenter.Grpc;

namespace Identity.HttpAPI.GrpcServices
{
    public class PermissionBindingGrpcService : PermissionBindingService.PermissionBindingServiceBase
    {
        private readonly IPermissionBindingService _permissionService;

        public PermissionBindingGrpcService(IPermissionBindingService permissionService)
        {
            _permissionService = permissionService;
        }

        public override async Task<GetBindingsResponse> GetBindings(GetBindingsRequest request, ServerCallContext context)
        {
            var bindings = await _permissionService.GetBindingsAsync(request.ServiceName, context.CancellationToken);

            var reply = new GetBindingsResponse();
            reply.Bindings.AddRange(bindings.Select(b => new PermissionBinding
            {
                ResourceId = b.resourceId,
                PermissionKey = b.permissionKey??string.Empty
                
            }));

            return reply;
        }
    }
}